package com.party.web.web.controller.notify;

import com.google.common.collect.Lists;
import com.party.common.paging.Page;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.notify.TargetTemplateType;
import com.party.core.model.notify.TargetTemplateWithQuery;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.web.biz.notify.MessageTemplateBizService;
import com.party.web.web.annotation.ControlParam;
import com.party.web.web.annotation.DataControl;
import com.party.web.web.dto.AjaxResult;
import com.party.web.web.dto.output.notify.TemplateOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 短信模板控制器
 * Created by wei.li
 *
 * @date 2017/10/12 0012
 * @time 9:29
 */
@Controller
@RequestMapping(value = "notify/template")
public class MessageTemplateController {

    @Autowired
    private MessageTemplateBizService messageTemplateBizService;
    @Autowired
    private IActivityService activityService;
    @Autowired
    private ICrowdfundEventService crowdfundEventService;


    /**
     * 模板列表
     * @param targetTemplateWithQuery 查询参数
     * @param page 分页参数
     * @return 交互数据
     */
    @DataControl
    @RequestMapping(value = "list")
    public ModelAndView list(@ControlParam(table = "activity", column = "member", getMethodName = "getTargetId") TargetTemplateWithQuery targetTemplateWithQuery, Page page){
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("/crowdfund/tempaleteList");
        if (null == targetTemplateWithQuery.getSendStatus()) {
            targetTemplateWithQuery.setSendStatus(TargetTemplateWithQuery.SEND_STATUS_NO);
        }
        List<TemplateOutput> list = messageTemplateBizService.list(targetTemplateWithQuery, page);
        Map<Integer, String> types = TargetTemplateType.convertMap(0);
        modelAndView.addObject("list", list);
        modelAndView.addObject("types", types);
        modelAndView.addObject("page", page);
        Activity activity = activityService.get(targetTemplateWithQuery.getTargetId());
        modelAndView.addObject("sendStatus", targetTemplateWithQuery.getSendStatus());
        modelAndView.addObject("targetId", targetTemplateWithQuery.getTargetId());
        modelAndView.addObject("activity", activity);
        return modelAndView;
    }

    /**
     * 会员管理
     * @param targetTemplateWithQuery 查询参数
     * @param page 分页参数
     * @return 交互数据
     */
    @DataControl
    @RequestMapping(value = "membermgmt/list")
    public ModelAndView membermgmtList(@ControlParam(table = "partner_member", column = "partner_id", getMethodName = "getTargetId")
                                                   TargetTemplateWithQuery targetTemplateWithQuery, Page page){
        page.setLimit(10);
        String targetId = "kc" + targetTemplateWithQuery.getTargetId();
        String parentId = targetTemplateWithQuery.getTargetId();
        targetTemplateWithQuery.setTargetId(targetId);

        ModelAndView modelAndView = new ModelAndView("member/mgmttempaleteList");
        if (null == targetTemplateWithQuery.getSendStatus()) {
            targetTemplateWithQuery.setSendStatus(TargetTemplateWithQuery.SEND_STATUS_NO);
        }
        List<TemplateOutput> list = messageTemplateBizService.list(targetTemplateWithQuery, page);
        list.remove(null);
        modelAndView.addObject("list", list);

        modelAndView.addObject("sendStatus", targetTemplateWithQuery.getSendStatus());
        modelAndView.addObject("targetId",parentId);
        modelAndView.addObject("page", page);

        return modelAndView;
    }


    /**
     * 活动模板列表
     * @param targetTemplateWithQuery 查询参数
     * @param page 分页参数
     * @return 交互数据
     */
    @DataControl
    @RequestMapping(value = "activity/list")
    public ModelAndView activityList(@ControlParam(table = "activity", column = "member", getMethodName = "getTargetId") TargetTemplateWithQuery targetTemplateWithQuery, Page page){
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("/activity/tempaleteList");
        if (null == targetTemplateWithQuery.getSendStatus()) {
            targetTemplateWithQuery.setSendStatus(TargetTemplateWithQuery.SEND_STATUS_NO);
        }
        ArrayList<Integer> typeList = Lists.newArrayList();
        typeList.add(TargetTemplateType.INTEGRAL_STATUS_ALL.getCode());
        typeList.add(TargetTemplateType.INTEGRAL_STATUS_CHECK.getCode());

        targetTemplateWithQuery.setTypes(typeList);
        List<TemplateOutput> list = messageTemplateBizService.list(targetTemplateWithQuery, page);
        Map<Integer, String> types = TargetTemplateType.convertMap(1);
        types.remove(TargetTemplateType.ACT_STATUS_ALL.getCode());
        modelAndView.addObject("list", list);
        modelAndView.addObject("types", types);
        modelAndView.addObject("sendStatus", targetTemplateWithQuery.getSendStatus());
        modelAndView.addObject("targetId", targetTemplateWithQuery.getTargetId());
        modelAndView.addObject("page", page);
        return modelAndView;
    }


    /**
     * 初始化短信模板
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "init")
    public AjaxResult init(){
        messageTemplateBizService.init();
        return AjaxResult.success();
    }


    /**
     * 模板列表
     * @param targetTemplateWithQuery 查询参数
     * @param page 分页参数
     * @return 交互数据
     */
    @DataControl
    @RequestMapping(value = "listForEvent")
    public ModelAndView listForEvent(@ControlParam(table = "zc_event", column = "create_by", getMethodName = "getTargetId") TargetTemplateWithQuery targetTemplateWithQuery, Page page){
        page.setLimit(10);
        ModelAndView modelAndView = new ModelAndView("/crowdfund/tempaleteListForEvent");
        if (null == targetTemplateWithQuery.getSendStatus()) {
            targetTemplateWithQuery.setSendStatus(TargetTemplateWithQuery.SEND_STATUS_NO);
        }
        List<TemplateOutput> list = messageTemplateBizService.list(targetTemplateWithQuery, page);
        Map<Integer, String> types = TargetTemplateType.convertMap(0);
        modelAndView.addObject("list", list);
        modelAndView.addObject("types", types);
        modelAndView.addObject("page", page);
        CrowdfundEvent crowdfundEvent = crowdfundEventService.get(targetTemplateWithQuery.getTargetId());
        modelAndView.addObject("crowdfundEvent", crowdfundEvent);
        modelAndView.addObject("sendStatus", targetTemplateWithQuery.getSendStatus());
        modelAndView.addObject("targetId", targetTemplateWithQuery.getTargetId());
        return modelAndView;
    }
}
